package com.airbnb.lottie.animation.content;

import static com.airbnb.lottie.LottieProperty.STROKE_COLOR;

import com.airbnb.lottie.LottieProperty;
import com.airbnb.lottie.animation.keyframe.BaseKeyframeAnimation;
import com.airbnb.lottie.animation.keyframe.ColorKeyframeAnimation;
import com.airbnb.lottie.animation.keyframe.ValueCallbackKeyframeAnimation;
import com.airbnb.lottie.LottieDrawable;
import com.airbnb.lottie.model.content.ShapeStroke;
import com.airbnb.lottie.model.layer.BaseLayer;
import com.airbnb.lottie.value.LottieValueCallback;

import ohos.agp.render.Canvas;
import ohos.agp.render.ColorFilter;
import ohos.agp.utils.Color;
import ohos.agp.utils.Matrix;
import org.jetbrains.annotations.Nullable;

public class StrokeContent extends BaseStrokeContent {

    private final BaseLayer layer;
    private final String name;
    private final boolean hidden;
    private final BaseKeyframeAnimation<Integer, Integer> colorAnimation;
    @Nullable
    private BaseKeyframeAnimation<ColorFilter, ColorFilter> colorFilterAnimation;

    public StrokeContent(final LottieDrawable lottieDrawable, BaseLayer layer, ShapeStroke stroke) {
        super(lottieDrawable,layer,stroke.getCapType().toPaintCap(),
            stroke.getJoinType().toPaintJoin(),stroke.getMiterLimit(),stroke.getOpacity(),
            stroke.getWidth(),stroke.getLineDashPattern(),stroke.getDashOffset());
        this.layer = layer;
        name = stroke.getName();
        hidden = stroke.isHidden();
        colorAnimation = stroke.getColor().createAnimation();
        colorAnimation.addUpdateListener(this);
        layer.addAnimation(colorAnimation);
    }

    @Override
    public void draw(Canvas canvas, Matrix parentMatrix, float parentAlpha) {
        if (hidden) {
            return;
        }
        paint.setColor(new Color(((ColorKeyframeAnimation) colorAnimation).getIntValue()));
        if (colorFilterAnimation != null) {
            paint.setColorFilter(colorFilterAnimation.getValue());
        }
        super.draw(canvas, parentMatrix, parentAlpha);
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public <T> void addValueCallback(T property, @Nullable LottieValueCallback<T> callback) {
        super.addValueCallback(property, callback);
        if (property == LottieProperty.STROKE_COLOR) {
            colorAnimation.setValueCallback((LottieValueCallback<Integer>) callback);
        } else if (property == LottieProperty.COLOR_FILTER) {
            if (colorFilterAnimation != null) {
                layer.removeAnimation(colorFilterAnimation);
            }

            if (callback == null) {
                colorFilterAnimation = null;
            } else {
                colorFilterAnimation =
                        new ValueCallbackKeyframeAnimation<>((LottieValueCallback<ColorFilter>) callback);
                colorFilterAnimation.addUpdateListener(this);
                layer.addAnimation(colorAnimation);
            }
        }
    }
}
